package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.query.RegionQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IRegionService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/region")
public class RegionController {
    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinationService destinationService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") RegionQuery qo, Model model) {
        PageInfo pageInfo = regionService.query(qo);
        System.out.println(pageInfo);
        model.addAttribute("pageInfo", pageInfo);
        return "/region/list";
    }

    @RequestMapping("/getDestByDeep")
    @ResponseBody
    public List getDestByDeep(int deep) {
        return destinationService.getDestsByDeep(deep);
    }

    @RequestMapping("/getDestByRegionId")
    @ResponseBody
    public List getDestByRegionId(Long rid) {
        return destinationService.getDestByRegionId(rid);
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public AjaxResult saveOrUpdate(Region region) {
        try {
                regionService.saveOrUpdate(region);
        } catch (Exception e) {
            return new AjaxResult(false,e.getMessage());
        }
        return new AjaxResult(true, "保存或编辑成功");
    }

    @RequestMapping("/changeHotValue")
    @ResponseBody
    public Object changeHotValue(boolean hot, Long id) {
        regionService.updateHotValue(hot, id);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(Long id) {
        try {
            regionService.delete(id);
        }catch (Exception e){
            return new AjaxResult(false,e.getMessage());
        }
        return new AjaxResult(true,"删除成功");
    }
}
